﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Common.Logging;
using Common.Model;
using ikvm.extensions;
using Newtonsoft.Json;
using sun.misc;

namespace Spider.Spider
{
    public class DadayouziWechatFupanSpider
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(DadayouziWechatFupanSpider));
        
        
        public static string Encjson (string json)
        {
            var random = new Random();
            var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
            StringBuilder stringBuilder = new StringBuilder();
            var _connkeylen = 20;
            for (int n = 0; _connkeylen > n; n++)
            {
                double ceiling = Math.Ceiling(62 * random.NextDouble());
                int o =Convert.ToInt32(ceiling);
                stringBuilder.Append(_keyStr[o]);
            }
            
            var s = stringBuilder.toString();
            var start = s.substring(0, 7);
            var stop = s.substring(6, 16);
            return start + json + stop;
        }
        
        public static List<DadayouziFupan> CrawlingDadayouziFupans()
        {
            
            string url = "http://www.dadayouzi.com/app/forum?"+Encjson("eyJhY3QiOiJ0b3BpY2xpc3QiLCJjb250YWluZXIiOiIjZnVwYW5saXN0IiwiZmlkIjoxMCwicGFnZSI6MSwidXNlcmlkIjowLCJkZXZpY2VpbmZvIjp7fX0=");

            var html = GetJson(url);
            var value = decode(html);
            var deserializeObject = JsonConvert.DeserializeObject<RootObject>(value);
            return deserializeObject.items.Select(m =>
            {
                var reqJson = "{\"act\":\"topic\",\"page\":1,\"tid\":\"" + m.topicid + "\",\"deviceinfo\":{}}";
                var encjson = Encjson(
                    Convert.ToBase64String(
                        Encoding.UTF8.GetBytes(reqJson)));
                string detialUrl = "http://www.dadayouzi.com/app/forum?" + encjson;
                var json = GetJson(detialUrl);
                var s = decode(json);
                RootObject2 detial = JsonConvert.DeserializeObject<RootObject2>(s);
                return new DadayouziFupan()
                {
                    Author = m.nickname,
                    Title = m.title,
                    Content = detial.topic.body.replaceAll("\"","\'"),
                    SiteTime = m.addtime,
                    CrawlingTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    Url = "http://www.dadayouzi.com/#!forum-topic-details?" + Encjson(
                              Convert.ToBase64String(
                                  Encoding.UTF8.GetBytes("{\"tid\":\"" + m.topicid + "\",\"page\":1}")))

                };
            }).ToList();
        }

        private static string GetJson(string url)
        {
            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
            request.Method = WebRequestMethods.Http.Get;
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
            request.UserAgent =
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36";
            var webResponse = request.GetResponse();

            Stream myResponseStream = null;
            StreamReader myStreamReader = null;
            string html = null;
            try
            {
                myResponseStream = webResponse.GetResponseStream();
                myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                html = myStreamReader.ReadToEnd();
            }
            catch (Exception e)
            {
                Log.Error(e);
                throw;
            }
            finally
            {
                myStreamReader?.Close();
                myResponseStream?.Close();
            }
            return html;
        }

        private static string decode(string html)
        {
            var base64Decoder = new BASE64Decoder();
            var decodeBuffer = base64Decoder.decodeBuffer(html.substring(7, html.length() - 10));
            string value = Encoding.UTF8.GetString(decodeBuffer);
            return value;
        }
    }

    public class Item
    {
        public string userid { get; set; }
        public string title { get; set; }
        public string topmost { get; set; }
        public string digest { get; set; }
        public string color { get; set; }
        public string replycount { get; set; }
        public string addtime { get; set; }
        public string lastreplytime { get; set; }
        public string topicid { get; set; }
        public string nickname { get; set; }
        public string icon { get; set; }
        public object rid { get; set; }
        public object rname { get; set; }
        public object ricon { get; set; }
    }

    public class RootObject
    {
        public int error { get; set; }
        public string msg { get; set; }
        public int forumid { get; set; }
        public string container { get; set; }
        public int userid { get; set; }
        public int pageindex { get; set; }
        public int pagecount { get; set; }
        public List<Item> items { get; set; }
    }

    public class Topic
    {
        public string topicid { get; set; }
        public string body { get; set; }
        public string topmost { get; set; }
        public string digest { get; set; }
        public string color { get; set; }
        public string title { get; set; }
        public string userid { get; set; }
        public string addtime { get; set; }
        public string replycount { get; set; }
        public string nickname { get; set; }
        public string icon { get; set; }
    }

    public class RootObject2
    {
        public int error { get; set; }
        public string msg { get; set; }
        public int pagecount { get; set; }
        public int pageindex { get; set; }
        public Topic topic { get; set; }
    }

}